home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / map / convert.c next >
Encoding:
C/C++ Source or Header  |  1992-11-12  |  3.6 KB  |  205 lines

  1. /*
  2.  *    convert.c
  3.  *
  4.  *    Convert "The World Digitized" "MPS" binary format files
  5.  *    to a Micro Doc "Micro World Data Bank II" compatible format.
  6.  *    The resulting database can be used with the MAP program.
  7.  *
  8.  *    Version 1.0 By Steve R. Sampson, Public Domain (p) February 1989
  9.  *
  10.  *    Compiled with Turbo-C 1.5
  11.  */
  12.  
  13. #include <dir.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16.  
  17. /*
  18.  *      fgets.c
  19.  *
  20.  *      Same as library fgets() only this version deletes '\n'
  21.  */
  22.  
  23. char *fgets(s, n, iop)
  24. char   *s;
  25. int    n;
  26. FILE   *iop;
  27. {
  28.         int    c;
  29.         char   *cs;
  30.  
  31.         cs = s;
  32.         while (--n > 0 && (c = getc(iop)) != EOF)  {
  33.                 if (c == '\n')  {
  34.                         *cs = '\0';
  35.                         break;
  36.                 } else
  37.                         *cs++ = c;
  38.         }
  39.  
  40.         return((c == EOF && cs == s) ? (char *)NULL : s);
  41. }
  42.  
  43. void convert(fpd, name, n)
  44. FILE    *fpd;
  45. char    *name;
  46. int    n;
  47. {
  48.     FILE    *fpi;
  49.     float    lat, lon;
  50.     int    newseg, c;
  51.  
  52.     if ((fpi = fopen(name, "rb")) == (FILE *)NULL)  {
  53.         printf("Unable to open %s\n", name);
  54.         return;
  55.     }
  56.  
  57.     puts(name);
  58.  
  59.     /*  Read and process the binary map "MPS" file */
  60.  
  61.     newseg = 1;
  62.     while (fread(&lat, 4, 1, fpi) != 0)  {
  63.         if (fread(&lon, 4, 1, fpi) < 1)  {
  64.             printf("Database error on file %s\n", name);
  65.             fclose(fpi);
  66.             return;
  67.         }
  68.  
  69.         if (newseg)  {
  70.             newseg = 0;
  71.             putw(n++, fpd);        /* increment segment number */
  72.         }                /* this is for future purp. */
  73.         else
  74.             putw(5, fpd);        /* low resolution default */
  75.  
  76.         /*
  77.          *    Convert degrees to minutes
  78.          */
  79.  
  80.         putw((int)(lat * 60.0F), fpd);
  81.         putw((int)(lon * 60.0F), fpd);
  82.  
  83.         /*
  84.          *    Scan past information text
  85.          */
  86.  
  87.         while ((c = fgetc(fpi)) != 0x0A)  {
  88.  
  89.             /*
  90.              *    0x01 value means end of segment
  91.              */
  92.  
  93.             if (c == 0x01)  {
  94.                 fgetc(fpi);    /* get the following LF */
  95.                 newseg = 1;
  96.                 break;
  97.             }
  98.         }
  99.     }
  100.  
  101.     fclose(fpi);
  102. }
  103.  
  104.  
  105. main(argc, argv)
  106. int    argc;
  107. char    *argv[];
  108. {
  109.     char    current[128], wrkcmd[80];
  110.     FILE    *fpw, *fpd;
  111.     int    i;
  112.  
  113.     if (argc != 2)  {
  114.        printf("Usage: convert database_name\n\n");
  115.        printf("Where \"database_name\" is the MWDBII compatible result\n");
  116.        exit(0);
  117.     }
  118.  
  119.     /*
  120.      *    Open the work file
  121.      */
  122.  
  123.     if ((fpw = fopen("convert.wrk", "r")) == (FILE *)NULL)  {
  124.         printf("Your missing \"convert.wrk\", I quit\n");
  125.         exit(1);
  126.     }
  127.  
  128.     /*
  129.      *    Open the output database file
  130.      */
  131.  
  132.     if (access(argv[1], 0) == 0)  {
  133.         printf("Output file '%s' already exists.\n", argv[1]);
  134.         exit(1);
  135.     }
  136.  
  137.     if ((fpd = fopen(argv[1], "wb")) == NULL)  {
  138.         printf("Unable to create %s\n", argv[1]);
  139.         exit(1);
  140.     }
  141.  
  142.     /*
  143.      *    Get current directory and convert to lower
  144.      *    case.  Capitals hurt my eyes...
  145.      */
  146.  
  147.     current[0] = '\\';
  148.     getcurdir(0, ¤t[1]);
  149.     for (i = 1; i < strlen(current); i++)
  150.         current[i] = tolower(current[i]);
  151.  
  152.     /*
  153.      *    Perform the work
  154.      */
  155.  
  156.     for (;;)  {
  157.         char    tmp[128];
  158.  
  159.         /*
  160.          *    Read work file line, and parse
  161.          */
  162.  
  163.         if (fgets(wrkcmd, sizeof wrkcmd, fpw) == NULL)
  164.             break;
  165.  
  166.         if ((wrkcmd[0] == '\0') || (wrkcmd[0] == '#'))
  167.             continue;
  168.         else if (wrkcmd[0] == '!')  {
  169.             strcpy(tmp, current);
  170.             strcat(tmp, "\\");
  171.             strcat(tmp, &wrkcmd[1]);
  172.  
  173.             if (chdir(tmp) == -1)  {
  174.                 printf("Can't change dir to %s\n", tmp);
  175.                 fclose(fpw);
  176.                 fclose(fpd);
  177.                 chdir(current);
  178.                 exit(1);
  179.             }
  180.  
  181.             printf("%s\n", tmp);
  182.  
  183.             continue;
  184.         }
  185.         else  {
  186.             char    *n;
  187.             int    code;
  188.  
  189.             n = strchr(wrkcmd, ',');
  190.             *n = '\0';
  191.  
  192.             code = atoi(++n);
  193.             convert(fpd, wrkcmd, code);
  194.         }
  195.     }
  196.  
  197.     fclose(fpw);
  198.     fclose(fpd);
  199.     chdir(current);
  200.  
  201.     exit(0);
  202. }
  203.  
  204. /* EOF */
  205.